var express = require('express');
var userModel = require("./../database/adminModel");
var router = express.Router();
const bcrypt = require('bcrypt');

/* GET users listing. */
router.get('/list', async function (req, res, next) {
  try {
    let { no, username,pageSize,current } = req.query;//获取传来的name,address
    let obj = {};
    if (username) {
      obj.username = new RegExp(username);//正则表达式实现模糊查询
    }
    if (no) {
      obj.no = new RegExp(no);//正则表达式实现模糊查询
    }
    //查询对应页的数据
    let skip =(current-1)*pageSize//从哪一条数据开始
    // let result = await studentModel.find(obj);//没有分页
    let result=await userModel.find(obj).skip(skip).limit(pageSize);//分页的
    let total=await userModel.find(obj).countDocuments();//总条数
    console.log("total",total)
    res.send({ code: 0, data: result, message: "成功",count:total });
  } catch (error) {
    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

router.get('/del', async function (req, res, next) {
  try {
    let { _id } = req.query;
    let result = await userModel.deleteOne({ "_id": _id });
    if (result.deletedCount > 0) {
      res.send({ code: 0, message: "删除成功" })
    }
    else {
      res.send({ code: 1, message: "删除失败" })
    }
  } catch (error) {
    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

//根据id查找对应的数据
router.get('/getByid', async function (req, res, next) {
  try {
    let { id } = req.query;
    let result = await userModel.findOne({ "_id": id })//查找的键名叫"_id" 查到的id给上面的id
    console.log(req.query);
    console.log(result);
    res.send({ code: 0, message: "成功", data: result });

  } catch (error) {
    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

//根据id更新数据
router.post('/update', async function (req, res, next) {
  try {
    var { id, no, username, password } = req.body
    if (password) {
      password = bcrypt.hashSync(password, 10);
    }
    var updateData = { no, username };
    if (password) {
      updateData.password = password;
    }
    var result = await userModel.updateOne({ "_id": id }, { $set: updateData });
    console.log(req.query)
    console.log(result);
    if (result.modifiedCount > 0) {
      res.send({ code: 0, message: "修改成功" })
    }
    else {
      res.send({ code: 0, message: "修改失败" });
    }
  } catch (error) {
    console.log(error);
    res.send({ code: 500, message: "服务器出错", detailMsg: error });
  }
});

// 注册 加密
router.post('/add', async function(req, res, next) {
  var { no, username, password } = req.body;
  password = bcrypt.hashSync(password, 10);
  try {
    var result = await userModel.insertMany([{ no, username, password }]);
    if (result.length > 0) {
      res.send({ code: 0, message: "注册成功" });
    } else {
      res.send({ code: 0, message: "注册失败" });
    }
  } catch(err) {
    console.log(err);
    res.send({ code: -1, message: "注册出错" });
  }
});

router.post("/login", async function(req, res) {
  try {
    let { username, password } = req.body;
    let result = await userModel.find({ username: username });
    if (result.length > 0) {
      const compareResult = bcrypt.compareSync(password, result[0].password);
      if (compareResult) {
        res.send({ code: 0, message: "登录成功" });
      } else {
        res.send({ code: 2, message: "密码错误" });
      }
    } else {
      res.send({ code: 1, message: "用户名不存在" });
    }
  } catch (err) {
    console.log(err);
    res.send({ code: -1, message: "登录出错" });
  }
});
module.exports = router;
